package com.ruoyi.exam.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.exam.domain.entity.ExamPaperQuestion;
import com.ruoyi.exam.domain.entity.TTaskExamPaper;
import com.ruoyi.exam.mapper.TTaskExamPaperMapper;

@RestController
@RequestMapping("/api")
public class TestsController {

    @Autowired
    private TTaskExamPaperMapper tTaskExamPaperMapper;

    @GetMapping("/test")
    @Anonymous
    public void test() {
        // 查询一条数据（可根据实际情况调整查询条件）
        List<TTaskExamPaper> papers = tTaskExamPaperMapper.selectTTaskExamPaperList(new TTaskExamPaper());
        if (papers.isEmpty()) {
            System.out.println("无数据");
            return;
        }
        TTaskExamPaper paper = papers.get(0);
        String paperContent = paper.getPaperContent();
        System.out.println("paperContent原始内容: " + paperContent);
        JSONObject jsonObject = JSON.parseObject(paperContent);
        JSONArray jsonArray = jsonObject.getJSONArray("examPaperQuestions");
        List<ExamPaperQuestion> examPaperQuestions = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject question = jsonArray.getJSONObject(i);
            ExamPaperQuestion examPaperQuestion = JSON.parseObject(question.toJSONString(), ExamPaperQuestion.class);
            if (examPaperQuestion.getQuestionText() == null || "[]".equals(examPaperQuestion.getQuestionText())
                    || "".equals(
                            examPaperQuestion.getQuestionText())) {
                examPaperQuestions.add(examPaperQuestion);
            }
        }
        System.out.println(examPaperQuestions.size());
    }

}
